package com.example.demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.example.demo.framework.base.BaseService;
import com.example.demo.framework.util.DoubleUtil;
import com.example.demo.framework.util.JodaUtil;
import com.example.demo.model.Fund;
import com.example.demo.model.UserInfo;
import com.example.demo.repository.FundRepository;
import com.example.demo.repository.UserRepository;

@Service
public class FundService extends BaseService {

	@Autowired
	private FundRepository fundRepository;
	
	@Autowired
	private UserRepository userRepository;
	
	@Transactional
	public String inpourHandle(String fundNo){
		Fund fund=fundRepository.getByFundNo(fundNo);
		if(fund==null){
			log.error("充值记录不存在,fundNo="+fundNo);
			return "error";
		}
		if(fund.getType()!=0){
			log.error("充值记录类型异常,fundNo="+fundNo);
			return "error";
		}
		if(fund.getStatus()==2){
			log.error("充值记录已审核,fundNo="+fundNo);
			return "error";
		}
		UserInfo user=userRepository.getByUserID(fund.getuID());
		fund.setStatus(2);
		fund.setUpdateTime(JodaUtil.now());
		fund.setBeforeMoney(user.getUserMoney());
		fund.setAfterMoney(DoubleUtil.add(user.getUserMoney(), fund.getChangeMoney()));
		user.setUserMoney(DoubleUtil.add(user.getUserMoney(), fund.getChangeMoney()));
		return "success";
	}

}
